package cn.lf.service.impl;

import cn.hutool.core.lang.UUID;
import cn.lf.constant.OrderStatusEnum;
import cn.lf.dto.api.order.UserOrderDetailRepDTO;
import cn.lf.dto.api.order.UserOrderInfoGetRepDTO;
import cn.lf.dto.api.order.UserOrderInfoGetReqDTO;
import cn.lf.dto.order.*;
import cn.lf.entity.OrderDetailInfo;
import cn.lf.entity.OrderInfo;
import cn.lf.entity.SysUser;
import cn.lf.mapper.OrderDetailInfoMapper;
import cn.lf.mapper.OrderInfoMapper;
import cn.lf.service.ApiOrderService;
import cn.lf.utils.SnowFlakeUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class ApiOrderServiceImpl implements ApiOrderService {

    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Resource
    private OrderDetailInfoMapper orderDetailInfoMapper;

    @Override
    public void createUserOrderInfo(UserOrderInfoGenReqDTO userOrderInfoGenReqDTO) {

        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();

        OrderInfo orderInfo = new OrderInfo();

        BeanUtils.copyProperties(userOrderInfoGenReqDTO,orderInfo);

        orderInfo.setOrderId(UUID.randomUUID().toString());

        orderInfo.setOrderNumber(String.valueOf(SnowFlakeUtil.generatorId()));

        orderInfo.setOrderStatus(OrderStatusEnum.UN_PAID.getCode());

        orderInfo.setUserId(sysUser.getUserId().toString());

        orderInfo.setCreateTime(new Date());

        orderInfo.setBuyerRate("0");

        orderInfo.setSourceType("2");

        orderInfoMapper.insert(orderInfo);

        OrderDetailInfo orderDetailInfo;

        for (UserOrderInfoGenReqDTO.productInfo productInfo : userOrderInfoGenReqDTO.getProductInfoList()) {

            orderDetailInfo = new OrderDetailInfo();

            orderDetailInfo.setOrderDetailId(UUID.randomUUID().toString());
            orderDetailInfo.setOrderId(orderInfo.getOrderId());
            orderDetailInfo.setProductId(productInfo.getProductId());
            orderDetailInfo.setProductNumber(productInfo.getProductNumber());
            orderDetailInfo.setProductPrice(BigDecimal.valueOf(productInfo.getProductPrice()));

            orderDetailInfoMapper.insert(orderDetailInfo);

        }

    }

    @Override
    public IPage<UserOrderInfoGetRepDTO> getUserOrderInfo(UserOrderInfoGetReqDTO userOrderInfoGetReqDTO) {

        SysUser loginUser  = (SysUser) SecurityUtils.getSubject().getPrincipal();

        userOrderInfoGetReqDTO.setUserId(loginUser.getUserId().toString());

        Page<UserOrderInfoGetRepDTO> page = new Page<>(userOrderInfoGetReqDTO.getCurrentPage(), userOrderInfoGetReqDTO.getPageSize());

        IPage<UserOrderInfoGetRepDTO> result = orderInfoMapper.selectUserOrderInfo(page,userOrderInfoGetReqDTO);

        for (UserOrderInfoGetRepDTO record : result.getRecords()) {

            List<UserOrderInfoGetRepDTO.ProductInfo> productInfoList = orderDetailInfoMapper.selectUserOdiList(record.getOrderId());

            record.setProductInfoList(productInfoList);

        }

        return result;
    }

    @Override
    public UserOrderDetailRepDTO getUserOrderDetailById(String orderId) {

        UserOrderDetailRepDTO result = orderInfoMapper.selectUserOdiOiById(orderId);

        List<UserOrderDetailRepDTO.ProductInfo> productInfoList = orderDetailInfoMapper.selectUserOdOdiList(orderId);

        result.setProductInfoList(productInfoList);

        return result;
    }

    @Override
    public void paymentForOrder(PfoReqDTO pfoReqDTO) {

        OrderInfo orderInfo = orderInfoMapper.selectById(pfoReqDTO.getOrderId());

        orderInfo.setOrderStatus(OrderStatusEnum.PAID.getCode());

        orderInfo.setPaymentType(pfoReqDTO.getPaymentType());

        orderInfo.setActualPay(pfoReqDTO.getActualPay());

        orderInfoMapper.updateById(orderInfo);

    }

    @Override
    public void cancelOrder(CancelOrderReqDTO cancelOrderReqDTO) {

        OrderInfo orderInfo = orderInfoMapper.selectById(cancelOrderReqDTO.getOrderId());

        orderInfo.setOrderStatus(OrderStatusEnum.CANCEL.getCode());

        orderInfoMapper.updateById(orderInfo);

    }

    @Override
    public void applyOrderRefund(AorReqDTO aorReqDTO) {

        OrderInfo orderInfo = orderInfoMapper.selectById(aorReqDTO.getOrderId());

        orderInfo.setOrderStatus(OrderStatusEnum.REFUNDING.getCode());

        orderInfo.setRefundPay(aorReqDTO.getRefundPay());

        orderInfoMapper.updateById(orderInfo);

    }
}
